# -*- encoding: utf-8 -*-
from django.db.models.query_utils import Q
from django.shortcuts import render_to_response, get_list_or_404,\
    get_object_or_404
from noticia.models import Noticia
from django.conf import settings
from django.utils.safestring import mark_safe
from django.template.context import RequestContext

def listar(request):
    lista_noticias = get_list_or_404(Noticia.objects.all().order_by('-data')[:20])
    return render_to_response('noticia/listar.html',{'lista' : lista_noticias},context_instance=RequestContext(request))


def obterPorId(request,noticia_id):
    noticia = get_object_or_404(Noticia, id=noticia_id)
    noticia.descricao = mark_safe(noticia.descricao)
    return render_to_response('noticia/noticia.html', {'noticia':noticia},context_instance=RequestContext(request))

def obterPorSlug(request,noticia_slug):
    noticia = get_object_or_404(Noticia, slug=noticia_slug)
    noticia.descricao = mark_safe(noticia.descricao)
    return render_to_response('noticia/noticia.html', {'noticia':noticia},context_instance=RequestContext(request))


def buscar(request, search):
    lista_noticias = Noticia.objects.filter(Q(titulo__icontains=search) | Q(descricao__icontains=search))[:20]
    return render_to_response('noticia/buscar.html', {'lista' : lista_noticias},context_instance=RequestContext(request))